package com.dingup.provider.service.sys.impl;

import com.dingup.api.model.sys.SysPermissionGroupRole;
import com.dingup.api.model.sys.SysRole;
import com.dingup.api.model.sys.SysUser;
import com.dingup.api.model.sys.SysUserRole;
import com.dingup.provider.mapper.SysPermissionGroupRoleMapper;
import com.dingup.provider.mapper.SysRoleMapper;
import com.dingup.provider.mapper.SysUserRoleMapper;
import com.dingup.provider.service.log.RoleUpdateLogService;
import com.dingup.provider.service.sys.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by wancheng on 2018/7/24.
 */
@Service
public class SysRoleServiceImpl implements SysRoleService{

    @Autowired
    SysRoleMapper sysRoleMapper;

    @Autowired
    SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    SysPermissionGroupRoleMapper sysPermissionGroupRoleMapper;

    @Autowired
    RoleUpdateLogService roleUpdateLogService;

    @Override
    public Boolean insert() {
        return null;
    }

    @Override
    @Transactional
    public Boolean update(String userId,Integer[] roleIds,String createUserId) {
        sysUserRoleMapper.deleteByUserId(userId);
        for (Integer roleId:roleIds) {
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setSysUserId(userId);
            sysUserRole.setSysRoleId(roleId);
            sysUserRole.setCreateTime(new Date());
            sysUserRole.setCreateUserId(createUserId);
            sysUserRoleMapper.insertSelective(sysUserRole);
        }
        return null;
    }

    @Override
    public Boolean delete(Integer roleId) {
        return sysRoleMapper.deleteByPrimaryKey(roleId) == 1 ? true:false;
    }

    @Override
    public Boolean copy(Integer roleId,String roleName, String userId) {

        SysRole role = sysRoleMapper.selectByPrimaryKey(roleId);
        List<SysPermissionGroupRole> group = sysPermissionGroupRoleMapper.getByRoleId(roleId);
        role.setId(null);
        role.setName(roleName);
        role.setCreateTime(new Date());
        role.setCreateUserId(userId);
        sysRoleMapper.insertSelective(role);
        roleUpdateLogService.saveRoleLog(role.getId(),userId);
        group.stream().forEach(
                g->{
                    g.setId(null);
                    g.setSysRoleId(role.getId());
                    g.setStatus(true);
                    g.setCreateTime(new Date());
                    sysPermissionGroupRoleMapper.insertSelective(g);
                }
        );
        return null;
    }

    @Override
    public SysRole getById(Integer id) {
        return sysRoleMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<SysRole> getByUserId(String userId) {

        return sysRoleMapper.getRolesByUserId(userId);
    }

    @Override
    public Integer getRoleUserNum(Integer roleId) {
        return sysRoleMapper.getRoleUserNumByRoleId(roleId);
    }

    @Override
    public List<SysRole> get() {
        return null;
    }

    @Override
    public Boolean addUserRole() {
        return null;
    }

    @Override
    public Boolean deleteUserRole() {
        return null;
    }

    @Override
    public List<SysRole> getUserRole() {
        return null;
    }

    @Override
    public List<SysRole> getAll() {
        List<SysRole> all = sysRoleMapper.getAll();
        return all;

    }

    @Override
    public List<SysRole> getByCreateUserId(List<String> createUserIds) {
        List<SysRole> list = sysRoleMapper.getByCreateUserIds(createUserIds);
        return list;
    }
}
